# -*- coding: iso-8859-2 -*-

import re
import string

from authorshipanalyzer.authorslot import AuthorFiller
from authorshipanalyzer.utils import sortDictionaryByValue
from authorshipanalyzer.slot.features import Age
from authorshipanalyzer.slot.features import Status

class ProfileFeaturesFiller(AuthorFiller):
    '''
    Fill slot with character and content based features:
    subjects: list of subjects
    categories: list of categories
    age
    sex
    status
    '''

    def fill(self, authorslot):
        '''
        Fill a slot
        '''
        
        subjects = {}
        categories = {}
        
        age = {}
        
        manCounter = 0.0
        womanCounter = 0.0
        
        diacrtiticsYes = 0.0
        
        status = {}
        
        for slot in authorslot.slots:            
            self.extendDict(age, slot.age)
            
            if slot.useDiacritics == True:
                diacrtiticsYes += 1.0
            
            if slot.sex == "woman":
                womanCounter += 1.0
            elif slot.sex == "man":
                manCounter += 1.0
                
            self.extendDict(status, slot.status)
        
        if float(diacrtiticsYes / len(authorslot.slots)) > 0.5:
            authorslot.useDiacritics = True
        else:
            authorslot.useDiacritics = False
             
        size = float(len(authorslot.slots))
        for k, v in subjects.items():
            subjects[k] = v / size
            
        for k, v in categories.items():
            categories[k] = v / size
            
        authorslot.subjects = subjects
        authorslot.categories = categories
        
        # Set age as the most frequent tag of ABOVE_* and/or BELOW_* which does not
        # contradict
        maxAbove = None
        aboveVal = 0.0
        maxBelow = None
        belowVal = 0.0
        for a, val in age.items():
            if re.match("ABOVE.*", a):
                if val > aboveVal:
                    maxAbove = a
                    aboveVal = val
            else:
                if val > belowVal:
                    maxBelow = a
                    belowVal = val                
        
        if maxAbove != None and maxBelow != None:
            if Age.value(maxAbove) < Age.value(maxBelow):
                authorslot.age.append(maxAbove)
                authorslot.age.append(maxBelow)
            else:
                authorslot.age.append(maxAbove)
        elif maxAbove != None:
            authorslot.age.append(maxAbove)
        elif maxBelow != None:
            authorslot.age.append(maxBelow)

        if max(womanCounter, manCounter) == womanCounter and womanCounter > 0.0:
            authorslot.sex = "woman"
        elif manCounter > 0:
            authorslot.sex = "man"
        else:
            authorslot.sex = "unknown"
            
        # Status
        maxKey = None
        maxVal = 0.0
        for k, v in status.items():
            if v > maxVal:
                maxKey = k
                maxVal = v
                
        if maxKey != None:
#            print "!!!!!!!!!! " + maxKey        
            if maxKey == Status.STUDENT:
                authorslot.status.append(Status.STUDENT)
            elif maxKey == Status.PROFFESSOR:
                authorslot.status.append(Status.PROFFESSOR)
                authorslot.status.append(Status.SCIENTIST)
            elif maxKey == Status.DOCTOR:
                authorslot.status.append(Status.DOCTOR)
                authorslot.status.append(Status.SCIENTIST)
            elif maxKey == Status.MASTER_OF_SCIENCE:
                authorslot.status.append(Status.MASTER_OF_SCIENCE)
            elif maxKey == Status.ENGINEER:
                authorslot.status.append(Status.ENGINEER)   
            elif Status.SCIENTIST in status:
                authorslot.status.append(Status.SCIENTIST)
                if Status.PROFFESSOR in status:
                    authorslot.status.append(Status.PROFFESSOR)
                elif Status.DOCTOR in status:
                    authorslot.status.append(Status.DOCTOR)
                elif Status.MASTER_OF_SCIENCE in status:
                    authorslot.status.append(Status.MASTER_OF_SCIENCE)
            
        
        
        #authorslot.status = list(set(status.keys()))
            
            
    def addToDict(self, dict, key):
        '''
        Increment key if exists or create a new one and insert one
        '''
        if dict.has_key(key):
            dict[key] += 1.0
        else:
            dict[key] = 1.0
            
    def extendDict(self, dict, keys):
        '''
        Increment keys if exist or create a new ones and insert ones
        @param keys: list of keys
        '''
        for key in keys:
            self.addToDict(dict, key)
               